﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class OptionScreen : ComponentView
    {
        bool isLeftClick = false;
        bool isPressEsc = false;
        private bool isBackMenuScreen = false;

        Game1 _game;

        Button _back;
        Button _ok;
        Label _labelDifficult;
        Label _labelSound;
        Label _labelResolution;
        Label _labelFullscreen;
        ImageView _backgroundMenu;

        Combobox _comboboxDifficult;
        Slide _slideSoundValue;
        Combobox _comboboxResolution;
        CheckBox _checkboxFullScreen;

        ComponentView _panel;

        public OptionScreen(Game1 game)
        {
            _game = game;

            Size = new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight);
            _backgroundMenu = new ImageView(@"Resources/background6", new Vector2(0, 0), 1);

            Vector2 sizePanel = new Vector2(GameBase.Constant.screenWidth - 200, GameBase.Constant.screenHeight - 200);
            Vector2 sizeGameScreen = new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight);

            _panel = new ComponentView(@"Resources/panel", new Vector2(100, -600), sizePanel);
            _panel.Alpha = 0.4f;
            _back = new Button(@"Resources/back", new Vector2(600, 430), 0.5f);
            _ok = new Button(@"Resources/ok", new Vector2(850, 430), 0.5f);

            _labelDifficult = new Label(@"Resources/fontLarge", @"Difficult", new Vector2(100, 80), Color.Blue, 0.75f);
            _labelResolution = new Label(@"Resources/fontLarge", @"Resolution", new Vector2(100, 160), Color.Blue, 0.75f);
            _labelFullscreen = new Label(@"Resources/fontLarge", @"Full Screen", new Vector2(100, 240), Color.Blue, 0.75f);
            _labelSound = new Label(@"Resources/fontLarge", @"Sound", new Vector2(100, 320), Color.Blue, 0.75f);

            _slideSoundValue = new Slide(0, 100, new Vector2(400, 320), 250, Color.Red, 0.75f);

            _panel.Action = new MoveAction(@"panelActionDone", _panel, (sizeGameScreen - sizePanel) / 2, 10.0f, 4.0f);
            _panel.Action.start();

            _comboboxDifficult = new Combobox(GameBase.Constant.difficults, new Vector2(400, 88), 250, Color.Red, 0.75f);
            _comboboxResolution = new Combobox(GameBase.Constant.resolutions, new Vector2(400, 168), 250, Color.Red, 0.75f);
            _checkboxFullScreen = new CheckBox(@"Resources/checkbox", @"Resources/uncheckbox", GameBase.Constant.isFullScreen, Vector2.Zero, 1);
            _checkboxFullScreen.Position = new Vector2(400 + _comboboxDifficult.Size.X / 2 - _checkboxFullScreen.Size.X/2, 240 + 5);

            this.addToSubView(_backgroundMenu);
            this.addToSubView(_panel);            
            _panel.addToSubView(_back);
            _panel.addToSubView(_ok);
            _panel.addToSubView(_labelDifficult);
            _panel.addToSubView(_labelResolution);
            _panel.addToSubView(_labelSound);
            _panel.addToSubView(_comboboxDifficult);
            _panel.addToSubView(_comboboxResolution);
            _panel.addToSubView(_slideSoundValue);
            _panel.addToSubView(_labelFullscreen);
            _panel.addToSubView(_checkboxFullScreen);

            this.subscribe(_panel.Action);

            GameBase.Constant.volume = 1;
        }

        public override void update(GameTime gameTime)
        {
            #region Mouse processing
            MouseState ms = Mouse.GetState();
            Vector2 targetPos = new Vector2(ms.X, ms.Y);

            if (ms.LeftButton == ButtonState.Pressed)
                isLeftClick = true;
            if (ms.LeftButton == ButtonState.Released && isLeftClick)
            {
                isLeftClick = false;
                if (_back.isContainPoint(targetPos))
                {
                    isBackMenuScreen = true;
                    reverseAllAction();
                }
                else if (_ok.isContainPoint(targetPos))
                {
                    isBackMenuScreen = true;

                    // update layout for new resolution
                    updateLayoutForNewResolution();
                   
                    reverseAllAction();

                    string [] resolution = _comboboxResolution.Items[_comboboxResolution.SelectedIndex].Text.Split('x');
                    if (resolution.Length == 2)
                    {
                        GameBase.Constant.screenWidth = Int32.Parse(resolution[0]);
                        GameBase.Constant.screenHeight = Int32.Parse(resolution[1]);
                    }

                    GameBase.Constant.isFullScreen = _checkboxFullScreen.IsChecked;                    
                    
                    GameBase.ConfigsReader.applyResolution();

                    MediaPlayer.Volume = GameBase.Constant.volume = _slideSoundValue.CurrentValue/100;
                } 
            }
            #endregion

            #region KeyBoard processing
            KeyboardState kbs = Keyboard.GetState();
            if (kbs.IsKeyDown(Keys.Escape))
                isPressEsc = true;
            if (kbs.IsKeyUp(Keys.Escape) && isPressEsc)
            {
                isPressEsc = false;
                isBackMenuScreen = true;
                reverseAllAction();
            }
            #endregion

            base.update(gameTime);
        }

        private void updateLayoutForNewResolution()
        {
            // Get new size of game screen.
            string[] str = _comboboxResolution.Items[_comboboxResolution.SelectedIndex].Text.Split('x');
            Vector2 newSizeGameScreen = new Vector2(Int32.Parse(str[0]), Int32.Parse(str[1]));

            // update panel action end position
            _panel.Action.EndPosition = new Vector2((newSizeGameScreen.X - _panel.Size.X) / 2, _panel.Action.EndPosition.Y);
            _panel.Action.BeginPosition = new Vector2((newSizeGameScreen.X - _panel.Size.X) / 2, _panel.Action.BeginPosition.Y);
        }

        private void reverseAllAction()
        {
            _panel.Action.reverse();
            _panel.Action.start();
        }

        public void playAnimation()
        {
            reverseAllAction();
        }

        #region ISubscriber
        public override bool notify(string message)
        {
            base.notify(message);

            if (isBackMenuScreen)
            {
                Game1.currentGameScreen = Game1.GameScreen.Menu;
                _game.MenuScreen.playAnimation();
            }
            isBackMenuScreen = false;
            return true;
        }
        #endregion
    }
}
